package com.taichuan.selfcheck.mvp.presenter;

import android.content.Context;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.widget.Button;
import android.widget.Toast;
import com.taichuan.mvp.base.MvpBasePresenter;
import com.taichuan.selfcheck.mvp.viewinterface.MicrophoneInterface;
import com.taichuan.selfcheck.utils.AudioRecordManager;

public class MicrophonePresenter extends MvpBasePresenter<MicrophoneInterface> {

    boolean hasRecord = false;
    boolean hasPlay = false;
    //存储录音工具类
    AudioRecordManager recordManager;
    //设置音量
    AudioManager manager;
    private Context context;
    private int currentStreamVolume;
    private Ringtone mRingtone;


    public MicrophonePresenter(Context context) {
        this.context = context;
        initAudioManager(context);
    }

    public void initAudioManager(Context context){
        recordManager = AudioRecordManager.getInstance();
        manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        //获取系统当前声音
        currentStreamVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);

        int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);


        manager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume,0);

        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        mRingtone = RingtoneManager.getRingtone(context, uri);
    }

    Button RecordButton;

    public void RecordSound(Button button){
        if(recordManager.isPlayingAudio()){
            getView().showMsg("正在播放录音中，请稍后再进行录音...");
        }else{
            RecordButton = button;
            if (button.getText().equals("2录音")) {
                button.setText("2停止");
                RecordButton.setText("2停止");
                recordManager.startRecord("data/data/com.taichuan.selfcheck/testMusic.pcm");
            }else {
                button.setText("2录音");
                RecordButton.setText("2录音");
                recordManager.stopRecord();
                getView().playSound("data/data/com.taichuan.selfcheck/testMusic.pcm");
                hasRecord = true;
            }
        }

    }
    public void PlaySound(Context context){
        try {
            if (RecordButton.getText().equals("2录音")) {
                if (hasRecord){
                    recordManager.startPlayThread();
                    if (hasRecord) {
                        hasPlay = true;
//                        CheckEventCode.Microphone_hasChecked = true;
                    }
                }
            }else {
                Toast.makeText(context,"请先停止录音", Toast.LENGTH_SHORT).show();
            }
        }catch (Exception e){
            Toast.makeText(context,"请先录音", Toast.LENGTH_SHORT).show();
        }
    }

    public void setStreamVolume(int streamType, int index, int flags) {
        currentStreamVolume = index;
        manager.setStreamVolume(streamType, index, flags);
    }

    public void playVolume() {
        if (mRingtone != null) {
            mRingtone.setStreamType(AudioManager.STREAM_MUSIC);
            mRingtone.play();
        }
    }

    public void stopRing(){
        if (mRingtone != null) {
            mRingtone.stop();
        }
    }


    @Override
    public void detachView() {
        super.detachView();
        if (mRingtone != null) {
            mRingtone.stop();
        }
    }

    public void StopAll(){
        if (recordManager != null){
            recordManager.stopRecord();
            recordManager.stopPlay();
        }
        if (manager != null){
            //复原设备原本的音量
            manager.setStreamVolume(AudioManager.STREAM_MUSIC,currentStreamVolume,0);
        }
    }

    public void initSeekBar() {
        getView().initSeekBar(currentStreamVolume);
    }
}
